/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SMART_SERVE_SERVICES_SERVER_SERVICE_CLIENT_API_H
#define SMART_SERVE_SERVICES_SERVER_SERVICE_CLIENT_API_H

#include "nlohmann/json.hpp"
#include "service/task_center.h"
#include "task.h"
#include <string>

using json = nlohmann::json;

namespace OHOS {
namespace SmartServe {

struct Request {
    static constexpr const char* EVAL_LLM = "eval_llm";
    static constexpr const char* GET_LLM_RESULT = "get_llm_result";
    static constexpr const char* CANCEL = "cancel";
    static constexpr const char* LIST_MODELS = "list_models";

    pid_t pid;
    std::string type;
    json request;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Request, pid, type, request)

struct EvalLLMRequest {
    std::string model;
    std::string input;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(EvalLLMRequest, model, input)

struct EvalLLMResponse {
    TaskID id;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(EvalLLMResponse, id)

struct GetLLMResultRequest {
    TaskID id;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(GetLLMResultRequest, id)

struct GetLLMResultResponse {
    TaskID id;
    std::string content;
    std::string error;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(GetLLMResultResponse, id, content, error)

struct CancelRequest {
    TaskID id;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(CancelRequest, id)

struct CancelResponse {
    std::string status;
    TaskID id;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(CancelResponse, status, id)

struct SessionInfo {
    std::string id;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SessionInfo, id)

struct ListModelsResponse {
    std::string object;
    std::vector<SessionInfo> data;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(ListModelsResponse, object, data)

enum class ErrorType { BAD_REQUEST_ERROR, NOT_FOUND_ERROR };

inline const std::unordered_map<ErrorType, std::pair<std::string, std::string>> errorTable = {
    {ErrorType::BAD_REQUEST_ERROR, {"bad_request_error", "The request was malformed or missing parameters."}},
    {ErrorType::NOT_FOUND_ERROR, {"not_found_error", "Requested resource does not exist."}}};

struct ErrorResponse {
    struct ErrorInfo {
        std::string type;
        std::string message;
        std::string param;
    } error;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(ErrorResponse::ErrorInfo, message, type, param)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(ErrorResponse, error)

class ClientAPIHandler {
public:
    std::string HandleRequest(const std::string& request);

private:
    std::string HandleEvalLLMRequest(const Request& request);
    bool EvalLLM(std::shared_ptr<EvalLLMTask> task);

    std::string HandleGetLLMResultRequest(const Request& request);

    std::string HandleCancelRequest(const Request& request);

    std::string HandleListModelsRequest();

    TaskCenter tasks_;
};

} // namespace SmartServe
} // namespace OHOS

#endif // SMART_SERVE_SERVICES_SERVER_SERVICE_CLIENT_API_H
